package com.seefly.firstmall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seefly.firstmall.core.Page;
import com.seefly.firstmall.entity.Good;
import com.seefly.firstmall.entity.TreeNode;
import com.seefly.firstmall.service.IGoodService;
import com.seefly.firstmall.service.ITreeNodeService;
import com.seefly.firstmall.util.AppConfigParam;

@Controller
@RequestMapping(value = "/")
public class GoodController {

	@Autowired
	private IGoodService goodService;

	@Autowired
	private ITreeNodeService treeNodeService;

	@RequestMapping(value = "/catagory/{id}", method = RequestMethod.GET)
	public String getGoodsByCatagory(@PathVariable("id") Long id) {

		TreeNode treeNode = treeNodeService.getTreeNodeById(id);

		List<Good> goods = goodService.getGoodsByCatagory(treeNode, null);

		return "homt/list";
	}

	@RequestMapping(value = "/good/{id}", method = RequestMethod.GET)
	public String getGoodDetail(@PathVariable("id") Long id,Model model) {

		Good good = goodService.getGoodDetail(id);
		
		model.addAttribute("good",good);

		return "home/detail";
	}

	@RequestMapping(value = "/list/{id}/{page}", method = RequestMethod.GET)
	public String getBrandList(@PathVariable("id") Long id,@PathVariable("page") Integer pageId,Model model) {
		List<Good> goods = null;
		Page page = new Page();
		page.setCurrent(pageId==null?0:pageId);
		page.setPagesize(AppConfigParam.appConfig.getPageSize());
        goods = goodService.getGoodsByBrandId(id, page);
		model.addAttribute("goods", goods);
		return "home/list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/good_test/test", method = RequestMethod.GET)
	public String updateGoods() {

		goodService.importResource();


		return "home/detail";
	}
	
}
